package com.luoyuanxiang.admin;

import java.util.Arrays;
import java.util.Scanner;

/**
 * <p>
 * 上机测试题
 * </p>
 *
 * @author luoyuanxiang <p>luoyuanxiangvip.com</p>
 * @since 2019/7/9
 */
public class EvilCorp {

    public static void main(String[] args) {
        Shield shield = new Shield();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要屏蔽的单词,单词之间使用逗号分隔：");
        shield.setWord(scanner.nextLine());
        System.out.println("请输入需要屏蔽的前缀单词,单词之间使用逗号分隔：");
        shield.setPrefixWord(scanner.nextLine());
        System.out.println("请输入需要屏蔽的后缀单词,单词之间使用逗号分隔：");
        shield.setSuffixWord(scanner.nextLine());
        System.out.println("请输入需要替换的单词，单词之间使用逗号分隔（格式：key=value）");
        System.out.println("key为需要屏蔽的单词，value为需要替换的单词");
        shield.setReplaceWord(scanner.nextLine());
        System.out.println("请输入内容：");
        shield.setContent(scanner.nextLine());
        System.out.println(filterSuffixWord(filterPrefixWord(filterReplaceWord(filterWord(shield)))).getContent());
    }

    /**
     * 单词过滤
     * @param shield 实体类
     * @return shield
     */
    private static Shield filterWord(Shield shield) {
        if(shield.getWord() != null) {
            String[] word = shield.getWord().split(",");
            Arrays.stream(word).forEach(str -> {
                if(shield.getContent().contains(str)) {
                    shield.setContent(shield.getContent().replace(str, "XXXX"));
                }
            });
        }
        return shield;
    }

    /**
     *  根据前缀替换
     * @param shield 实体类
     * @return shield
     */
    private static Shield filterPrefixWord(Shield shield) {
        if(shield.getPrefixWord() != null) {
            String[] words = shield.getPrefixWord().split(",");
            String[] contents = shield.getContent().split(" ");
            Arrays.stream(contents).forEach(content -> Arrays.stream(words).forEach(word -> {
                if(content.contains(word)) {
                    shield.setContent(shield.getContent().replace(content, "XXXX"));
                }
            }));
        }
        return shield;
    }

    /**
     *  根据后缀替换
     * @param shield 实体类
     * @return shield
     */
    private static Shield filterSuffixWord(Shield shield) {
        if(shield.getSuffixWord() != null) {
            if(shield.getSuffixWord() != null) {
                String[] words = shield.getSuffixWord().split(",");
                String[] contents = shield.getContent().split(" ");
                Arrays.stream(contents).forEach(content -> Arrays.stream(words).forEach(word -> {
                    if(content.contains(word)) {
                        shield.setContent(shield.getContent().replace(content, "XXXX"));
                    }
                }));
            }
        }
        return shield;
    }

    /**
     * 单词替换
     * @param shield 实体类
     * @return 内容
     */
    private static Shield filterReplaceWord(Shield shield) {
        if(shield.getReplaceWord() != null) {
            String[] replaceWord = shield.getReplaceWord().split(",");
            Arrays.stream(replaceWord).forEach(str -> {
                String[] maps = str.split("=");
                if(shield.getContent().contains(maps[0])) {
                    shield.setContent(shield.getContent().replace(maps[0], maps[1]));
                }
            });
        }
        return shield;
    }
}

/**
 * 单词屏蔽类
 */
class Shield {

    /**
     * 单词屏蔽字符串
     */
    private String word;

    /**
     * 前缀单词替换
     */
    private String prefixWord;

    /**
     * 后缀单词替换
     */
    private String suffixWord;

    /**
     * 单词替换
     */
    private String replaceWord;

    /**
     * 内容
     */
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String getPrefixWord() {
        return prefixWord;
    }

    public void setPrefixWord(String prefixWord) {
        this.prefixWord = prefixWord;
    }

    public String getSuffixWord() {
        return suffixWord;
    }

    public void setSuffixWord(String suffixWord) {
        this.suffixWord = suffixWord;
    }

    public String getReplaceWord() {
        return replaceWord;
    }

    public void setReplaceWord(String replaceWord) {
        this.replaceWord = replaceWord;
    }
}
